package com.itheima.mp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.itheima.mp.domain.po.Address;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.vo.AddressVO;
import com.itheima.mp.enums.UserStatus;
import com.itheima.mp.mapper.AddressMapper;
import com.itheima.mp.service.IAddressService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王岩尘
 * @since 2024-06-22
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Override
    public List<AddressVO> getAddressByUserId(Long userId) {
        //判断用户状态
        User user = Db.lambdaQuery(User.class).eq(User::getId, userId).one();
        if (user == null || user.getStatus() == UserStatus.FROZEN){
            throw new RuntimeException("用户被冻结或用户状态异常");
        }
        //根据用户id查询地址
        List<Address> list = lambdaQuery().eq(Address::getUserId, userId).list();
        return BeanUtil.copyToList(list, AddressVO.class);
    }
}
